package 二叉树;

/**
 * 最大深度使用前序遍历
 */
public class 二叉树的最大深度 {

    public int maxDepth(TreeNode root){
        return handle(root, 0);
    }

    public int handle(TreeNode node, int depth){
        if(node == null){
            return depth;
        }
        depth++;
        int leftDepth = handle(node.left, depth);
        int rightDepth = handle(node.right, depth);
        return Math.max(leftDepth, rightDepth);
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
